import { createApp } from "vue";
import { setupRouter } from "@/router";
import { setupStore } from "@/store";
import { useGlobalStore, store } from '@/store'
import {
  setupCustomComponents,
  setupGlobalMethods,
  setupDirectives,
  setupAssets,
} from "@/plugins";
import App from "./App.vue";
async function bootStrap() {
  // 设置样式加载
  setupAssets();
  // 创建vue
  const app = createApp(App);
  // 全局加载
  // 注册全局常用的 组件
  setupCustomComponents(app);

  // 注册全局方法
  setupGlobalMethods(app);

  // 注册全局自定义指令
  setupDirectives(app);

  // 挂载状态管理
  await setupStore(app);

  // 挂载路由
  await setupRouter(app);
  // 全局错误处理，捕获未定义变量错误
  window.addEventListener('error', (event) => {
    if (event.message.includes('login is not defined')) {
      console.warn('检测到 login 未定义错误，可能是宿主调用时未加引号。正确调用方式: dispatchCefEvent("login")')
      event.preventDefault() // 阻止错误冒泡
      return false
    }
  })

  // 全局CEF事件入口：转发到全局Store
  window.dispatchCefEvent = function (eventName: string, body?: any) {
    try {
      const globalStore = useGlobalStore(store)
      globalStore.handleCefEvent(eventName, body)
    } catch (e) {
      console.error('dispatchCefEvent error', e)
    }
  }

  // 为了兼容可能的错误调用，定义一个 login 变量
  ;(window as any).login = 'login'
  app.mount("#app");
}
bootStrap();


